package com.speech.net;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.ontbee.legacyforks.cn.pedant.SweetAlert.SweetAlertDialog;
import com.speech.activity.LoginActivity;
import com.speech.constant.PrefsConstant;
import com.speech.db.SharedPreferenceHelper;
import com.speech.net.entity.MessageS2C;
import com.speech.utils.AESEncryption;
import com.speech.utils.JsonProcessor;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class BasicCallBack implements Callback<ResponseBody> {

    private Context mContext;

    private MessageS2C mMessageS2C;

    private String strOriginRequestJson;

    private static boolean bFlagShowing_TimeOut = false;

    public BasicCallBack(Context context, String strOriginRequestJson, BasicCallBackListener listener) {
        mContext = context;
        mListener = listener;
        this.strOriginRequestJson = strOriginRequestJson;
    }

    public BasicCallBackListener mListener;

    public interface BasicCallBackListener {
        void success(MessageS2C msg_s2c);

        void fail(MessageS2C msg_s2c);
    }

    @Override
    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

        if (response.code() != 200) {
            //TODO:请求失败
            _show_connect_time_out_dialog();
            return;
        }

        String str = null;
        try {
            str = response.body().string();
            str = str.replace("(", "+").replace(")", "/").replace("@", "=");
        } catch (IOException e) {
            e.printStackTrace();
        }
        String json_s2c = AESEncryption.decrypt(str);
        mMessageS2C = JsonProcessor.StcGetObject(MessageS2C.class, json_s2c);

        Log.d("fzw", "服务器响应:" + mMessageS2C.resultCode);

        switch (mMessageS2C.resultCode) {
            case 成功:
                if (mListener != null) {
                    mListener.success(mMessageS2C);
                }
                break;
            case 登录过期:


                //TODO: 接口请求
                //登陆过期就进行token登陆
                HttpCommunicationManager.getInstance().doTokenLogin(mContext, strOriginRequestJson, this);
                break;
            case 需要登录:
                goLogin();
                break;
            case 设备需登记:


                //TODO: 接口请求
//                HttpCommunicationManager.getInstance().doSignInDevice(mContext, strOriginRequestJson, this);
                break;
            case 通用错误:
            case 邀请码错误:
            case 非法设备ID:
            case 注册手机已存在:
            case 这小时申请验证码太多:
            case 今日申请验证码太多:
            case 验证码错误:
            case 验证码超时:
            case 登录名不存在:
                mListener.fail(mMessageS2C);
                break;
            default:
                mListener.fail(mMessageS2C);
                break;
        }
    }

    private void reset() {
        new SweetAlertDialog(mContext, SweetAlertDialog.ERROR_TYPE)
                .setTitleText("发生未知错误")
                .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        SharedPreferenceHelper spf_helper = new SharedPreferenceHelper(mContext, PrefsConstant.FILE_USER_INFO);
                        spf_helper.clear();


                        spf_helper = new SharedPreferenceHelper(mContext, PrefsConstant.FILE_TODAY_DATA);
                        spf_helper.clear();

                        sweetAlertDialog.dismiss();
                        goLogin();
                    }
                })
                .setConfirmText("OK")
                .show();


    }

    @Override
    public void onFailure(Call<ResponseBody> call, Throwable t) {
        _show_connect_time_out_dialog();
    }

    private void _show_connect_time_out_dialog() {
        if (!bFlagShowing_TimeOut) {
            bFlagShowing_TimeOut = true;
            new SweetAlertDialog(mContext, SweetAlertDialog.ERROR_TYPE)
                    .setTitleText("连接超时")
                    .setContentText("服务器连接超时")
                    .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                        @Override
                        public void onClick(SweetAlertDialog sweetAlertDialog) {
                            bFlagShowing_TimeOut = false;
                            sweetAlertDialog.dismiss();
                        }
                    })
                    .setConfirmText("OK")
                    .show();
        }
    }


    private void goLogin() {
        //跳转到登录页并清空activity栈
        SharedPreferenceHelper spf_helper = new SharedPreferenceHelper(mContext, PrefsConstant.FILE_USER_INFO);
        spf_helper.putData(PrefsConstant.KEY_USER_INFO_INT_LOGIN_STATE, 0);
        mContext.startActivity(new Intent(mContext, LoginActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK));

    }
}
